package com.cqupt.scanning.system.acl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.scanning.system.acl.entity.AclRole;
import com.cqupt.scanning.system.acl.entity.vo.RoleVO;
import com.cqupt.scanning.system.acl.service.AclRoleService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Data: 2021/7/19 14:21
 * @Author: 宋宝梁
 */
@RestController
@RequestMapping("/admin/acl/role")
public class AclRoleController {

    private AclRoleService aclRoleService;

    @Autowired
    public AclRoleController(AclRoleService aclRoleService) {
        this.aclRoleService = aclRoleService;
    }

    /**
     * 新增角色（含分配权限）
     * @param roleVO
     * @return
     */
    @PostMapping("/addRole")
    @PreAuthorize("hasAuthority('role.addRole')")
    public R addRole(@RequestBody @Validated RoleVO roleVO) {
        return aclRoleService.addRole(roleVO);
    }

    /**
     * 根据id获取角色
     * @param roleId
     * @return
     */
    @GetMapping("/getOneRole/{id}")
    public R getOneRole(@PathVariable("id") Integer roleId) {
        return aclRoleService.getOneRole(roleId);
    }

    /**
     * 获取所有角色（支持分页和角色名模糊条件查询）
     * @param query
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAllRole")
    @PreAuthorize("hasAuthority('role.getAllRole')")
    public R getAllRole(@RequestParam("query") String query,
                        @RequestParam(value = "pageNum", defaultValue = "1") Integer current,
                        @RequestParam(value = "pageSize", defaultValue = "10") Integer size) {
        Page<AclRole> page = new Page<>(current, size);
        return aclRoleService.getAllRole(page, query);
    }

    /**
     * 修改角色
     * @param roleVO
     * @return
     */
    @PutMapping("/updateRole")
    @PreAuthorize("hasAuthority('role.updateRole')")
    public R updateRole(@RequestBody @Validated RoleVO roleVO) {
        return aclRoleService.updateRole(roleVO);
    }

    /**
     * 根据id删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping("/removeRole/{id}")
    @PreAuthorize("hasAuthority('role.removeRole')")
    public R removeRoleById(@PathVariable("id") @NotNull(message = "id cannot be empty") Integer roleId) {
        return aclRoleService.removeRoleById(roleId);
    }


}
